//
//  SYNetworkTools.swift
//  SYWeibo
//
//  Created by Eric on 16/7/22.
//  Copyright © 2016年 Eric. All rights reserved.
//

import UIKit
import AFNetworking

//请求方式的枚举
enum SYNetworkToolsType: String {

    case GET = "GET"
    case POST = "POST"

}

class SYNetworkTools: AFHTTPSessionManager  {
    //全局的访问点
    static let sharedTool: SYNetworkTools = {
    let tools = SYNetworkTools()
        //指定反序列化得格式
    tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
       
        return tools
    }()
    

    func request(type: SYNetworkToolsType, urlString: String,parameters: AnyObject?, success:(response: AnyObject?)->(),failure: (error: NSError)->()){
        if type == .GET{
        //get 的请求
            GET(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                success(response: res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
            })
        }else{
            //post的请求
            POST(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                success(response: res)
        
                }, failure: { (_, err) -> Void in
                    failure(error: err)
            })
        
        
        }
    
    
    }
    

}

//MARK :  -请求OAuth界面数据
extension SYNetworkTools{
    //请求 userAccount
func oauthLoadUserAccount(code: String, success:(response: AnyObject?)->(),failure: (error: NSError)->()){

    //请求urlString
    let urlString = "https://api.weibo.com/oauth2/access_token"
    //请求参数
    let params = [
        "client_id": APPKEY,
        "client_secret": APPSECRER,
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": APPREDIRECT_URL
    ]
    request(SYNetworkToolsType.POST, urlString: urlString, parameters: params, success: success, failure: failure)
}
    // 请求用户信息数据
    func oauthLoadUserInfo(userAccount: SYUserAccount, success: (response: AnyObject?)->(), failure: (error: NSError)->()){
        // 请求的urlString
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token": userAccount.access_token!,
            "uid": userAccount.uid!
        ]
        request(SYNetworkToolsType.GET, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}

//MARK: - 请求首页微博界面数据
extension SYNetworkTools{
    func homeLoadData(sinceId: Int64, maxId: Int64,success: (response: AnyObject?)->(), failure: (error: NSError)->()){
        // 请求urlString
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params = [
            "access_token": SYUserAccountViewModel.sharedTools.accessToken!,
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"
        
        ]

        request(SYNetworkToolsType.GET, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }
}














